package com.dengke.hellospringmvc.controller;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dengke.hellospringmvc.dao.QueryForListReturnListDAO;
import com.dengke.hellospringmvc.dao.QueryForRowSetReturnSqlRowSetDAO;
import com.dengke.hellospringmvc.dao.QueryWithRowMapperDAO;
import com.dengke.hellospringmvc.model.Department;

@Controller
@RequestMapping("/jdbc")
public class JdbcController {

	private QueryForListReturnListDAO dao;
	private QueryForRowSetReturnSqlRowSetDAO rowsetDao;
	private QueryWithRowMapperDAO rowMapperDao;

	@Autowired
	public JdbcController(QueryForListReturnListDAO queryForListReturnListDAO,
			QueryForRowSetReturnSqlRowSetDAO rowSetDAO, QueryWithRowMapperDAO rowMapperDAO) {
		this.dao = queryForListReturnListDAO;
		this.rowsetDao = rowSetDAO;
		this.rowMapperDao = rowMapperDAO;
	}

	@RequestMapping("/list")
	public String Jdbc(Model model) {
		List<String> names = this.dao.getDeptNames();
		for (String name : names) {
			System.out.println(name);
		}
		model.addAttribute("names", names);
		return "jdbc";
	}

	@RequestMapping("/rowset")
	public String rowSet(Model model) {
		SqlRowSet rowSet = this.rowsetDao.queryForRowSet_SqlRowSet();
		while (rowSet.next()) {
			System.out.println("--------");
			String title = rowSet.getString("Title");
			String description = rowSet.getString("Description");
			Date date = rowSet.getDate("ReleaseDate");
			String writer = rowSet.getString("Writer");
			System.out.println(title + "   " + description + "  " + date.toString() + "  " + writer);
		}
		return "jdbc";
	}

	@RequestMapping("/rowmapper")
	public String rowMapper(Model model) {
		List<Department> list = this.rowMapperDao.queryDepartment();
		for (Department department : list) {
			System.out.println(department.getDepName()+"  "+department.getDepId());
		}
		return "jdbc";
	}
}
